
local map = vim.api.nvim_set_keymap
local opt = {noremap = true, silent = true }

map("n", "<leader>e", ":NvimTreeToggle<CR>", opt)

map("n", "<leader>ff", ":Telescope find_files<CR>", opt )
map("n", "<leader>fw", ":Telescope live_grep<CR>", opt )
map("n", "<leader>fc", ":Telescope grep_string<CR>", opt )
map("n", "<c-l>", ":bnext<CR>", opt )
map("n", "<c-h>", ":bprevious<CR>", opt )


map("n", "<leader>s", ":SessionManager load_session<CR>", opt )
map("n", "<leader>Sl", ":SessionManager load_last_session<CR>", opt )
map("n", "<leader>Ss", ":SessionManager save_current_session<CR>", opt )
map("n", "<leader>Sd", ":SessionManager delete_session<CR>", opt )

map("n", "<leader>t", ":ToggleTerm direction=float<CR>", opt )
map("t", "<esc>", [[<C-\><C-n>]], opt )



function close_all_buf()
    --local n = vim.api.nvim_get_current_buf()
    --for _, bufnr in ipairs(vim.api.nvim_list_bufs() ) do
    --    if bufnr ~= n then
    --        vim.cmd( "bd!" .. bufnr )
    --    end
    --end

    local pre = 0
    for i = 1, 100, 1 do
        local n = vim.api.nvim_get_current_buf()
        vim.cmd( "bp | bd!" .. n )
        if n == pre then 
            print( "hit end ", i )
            break 
        end
        pre = n
    end
end


function close_the_buf()
    local n = vim.api.nvim_get_current_buf()
    vim.cmd( "bp | bd!" .. n )
end


map("n", "<leader>ba", ":lua close_all_buf()<CR>", opt )
map("n", "<leader>bd", ":lua close_the_buf()<CR>", opt )
